package com.kstech.mp3604.fragments

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.kstech.mp3604.adapter.ControlInitConfigAdapter
import com.kstech.mp3604.adapter.GridSpaceItemDecoration
import com.kstech.mp3604.base.BaseDialogFragment
import com.kstech.mp3604.base.Global.ANGLE
import com.kstech.mp3604.base.Global.INNER_ARC
import com.kstech.mp3604.base.Global.ONE_LINE_LENGTH
import com.kstech.mp3604.base.Global.OUTER_ARC
import com.kstech.mp3604.base.Global.TWO_LINE_LENGTH
import com.kstech.mp3604.bean.ControlInitData
import com.kstech.mp3604.databinding.FragmentDialogInitConfigBinding

/**
 * 控制器初始化参数配置
 */
class DialogInitConfigFragment :
    BaseDialogFragment<FragmentDialogInitConfigBinding>(FragmentDialogInitConfigBinding::inflate) {
    private lateinit var initConfigAdapter: ControlInitConfigAdapter
    private val dataList = ArrayList<ControlInitData>()
    override fun onInitView() {
        initView()
        initRecyclerView()
        initData()
    }

    private fun initView() {
        binding.apply {
            closeDialog.setOnClickListener {
                dialog!!.cancel()
            }
        }
    }

    private fun initData() {
        dataList.clear()
        var angle = SPUtils.getInstance().getFloat(ANGLE)

        val list = arrayOf<ControlInitData>(
            ControlInitData(
                "2100", 0, "口1长度", SPUtils.getInstance().getInt(ONE_LINE_LENGTH), "1mm"
            ), ControlInitData(
                "2101", 0, "口2长度", SPUtils.getInstance().getInt(TWO_LINE_LENGTH), "1mm"
            ), ControlInitData(
                "2102", 0, "内弧长度", SPUtils.getInstance().getInt(INNER_ARC), "1mm"
            ), ControlInitData(
                "2103", 0, "外弧长度", SPUtils.getInstance().getInt(OUTER_ARC), "1mm"
            ), ControlInitData(
                "2104", 0, "角度", (angle * 100).toInt(), "已乘100"
            )
        )
        dataList.addAll(list)
        initConfigAdapter.notifyDataSetChanged()
    }

    private fun initRecyclerView() {
        initConfigAdapter = ControlInitConfigAdapter()
        binding.recyclerView.layoutManager = LinearLayoutManager(requireActivity())
        binding.recyclerView.addItemDecoration(GridSpaceItemDecoration(10, 10))
        binding.recyclerView.adapter = initConfigAdapter
        initConfigAdapter.submitList(dataList)
    }
}